package de.tabuma.shopping.logik;

import de.tabuma.shopping.entity.*;
import java.util.logging.*;
import javax.jdo.*;
import java.util.*;
import java.util.logging.*;
/**
 *
 * @author $Author: alefen $
 * @version $Revision: 1.1 $
 * @since $Date: 2009/07/21 19:37:32 $
 */
public class CurrentOrderFacade {
    private static final Logger log = Logger.getLogger(CurrentOrderFacade.class.getName());
    
    public static CurrentOrder getCurrentOrder(String user) {
        String keys[] = {"user"};
        String values[] = {user};

        CurrentOrder currentOrder = null;

        List<CurrentOrder> coList = PMF.get(keys, values, new CurrentOrder());
        if(coList.size()!=0) {
            log.fine("In datastore there is a currentOrder.");
            currentOrder = coList.get(0);
        }
        if(currentOrder==null) {
            currentOrder = new CurrentOrder(user);
            PMF.doCreate(currentOrder, user);
        }
        return currentOrder;
    }

    private static CurrentOrder get(String user) {
        String keys[] = {"user"};
        String values[] = {user};
        List<CurrentOrder> coList = PMF.get(keys, values, new CurrentOrder());
        if(coList.size()>0) {
            return coList.get(0);
        }
        return null;
    }

    public static void addLicensePackageToCurrentOrder(String user,Integer LicenseOrderID) {
        CurrentOrder currentOrder = getCurrentOrder(user);
        CurrentOrderLicensePackageRelation colpr = new CurrentOrderLicensePackageRelation();
        colpr.setCurrentOrderId(currentOrder.getId().intValue());
        colpr.setLicensePackageId(LicenseOrderID.intValue());
        CurrentOrderLicensePackageRelationFacade.create(colpr, user);
    }

    public static void removeLicensePackageFromCurrentOrder(String user,int nr) {
        CurrentOrder currentOrder = get(user);
        List<CurrentOrderLicensePackageRelation> colpr = CurrentOrderLicensePackageRelationFacade.getList(currentOrder.getId().intValue());
        CurrentOrderLicensePackageRelationFacade.remove(colpr.get(nr), user);
    }

    public static void removeAllPackageFromCurrentOrder(String user) {
        CurrentOrder currentOrder = get(user);
        List<CurrentOrderLicensePackageRelation> colpr = CurrentOrderLicensePackageRelationFacade.getList(currentOrder.getId().intValue());
        for(int i=0;i<colpr.size();i++) {
            CurrentOrderLicensePackageRelationFacade.remove(colpr.get(i), user);
        }
    }

    public static void removeCurrentOrder(String user) {
        removeAllPackageFromCurrentOrder(user);
        CurrentOrder currentOrder = get(user);
        if(currentOrder!=null) {
            PMF.doRemove(currentOrder, user);
        }
    }

    public static int createOrder(String user) {
        int returnValue = 0;
        CurrentOrder currentOrder = get(user);
        if(currentOrder!=null) {
            Order myOrder = new Order();
            myOrder.setToken(Order.createToken());
            returnValue = OrderFacade.doCreate(myOrder, user);
            List<CurrentOrderLicensePackageRelation> colprs = CurrentOrderLicensePackageRelationFacade.getList(currentOrder.getId().intValue());
            for(int i=0;i<colprs.size();i++) {
                OrderLicensePackageRelation olpr = new OrderLicensePackageRelation();
                olpr.setOrderId(returnValue);
                olpr.setLicensePackageId(colprs.get(i).getLicensePackageId());
                OrderLicensePackageRelationFacade.create(olpr, user);
            }
        }
        removeCurrentOrder(user);
        return returnValue;
    }
}